home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].zip / MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].adf / Programming / Pascal_Tutes / Tute15.pas < prev    next >
Pascal/Delphi Source File  |  1995-01-27  |  8KB  |  274 lines

  1. {
  2.  
  3.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
  4.              «»                                                  «» 
  5.              «»                 TUTORIAL FIFTEEN                 «» 
  6.              «»                                                  «» 
  7.              «»                        by                        «» 
  8.              «»                                                  «» 
  9.              «»                   Anthony Peck                   «» 
  10.              «»                                                  «» 
  11.              «»                                                  «» 
  12.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 
  13.                                                                     
  14.  
  15.  
  16.          43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43
  17.  
  18.  
  19.                         Have you anything to declare?
  20.  
  21.  
  22.    Calling procedures can be done from anywhere in the program, with one 
  23.    limitation.  If you call a procedure that has yet to be defined, you 
  24.    must make a forward declaration.  Some programmers forward declare all 
  25.    of their procedures anyway, and that way they can swap them around 
  26.    without worrying which procedure is called when.
  27.  
  28.    We forward declare procedures using the directive "forward".  Eh?
  29.  
  30.    Let's do a program in which all the procedures have been forward 
  31.    declared, and thus can be called from anywhere in the program... 
  32.  
  33.  
  34.     PROGRAM MDTute15;
  35.  
  36.     {    Program works out pay given the hours and the rate
  37.  
  38.                 Author : A N Peck
  39.  
  40.                 Date : 1 November 1994 
  41.  
  42.         Procedures used:
  43.     
  44.         Title - displays program information
  45.  
  46.         Getnumber - returns user entry
  47.  
  48.         Calculate - finds the total payment due }
  49.  
  50.     Procedure Calculate; forward;
  51.  
  52. {    ^
  53.     |
  54.     |__  Here we declare a procedure called "calculate" and then give
  55.          the directive forward.  This is like Nostradamus predicting
  56.          the winner of the 1995 Australian Checkers Spectacular.
  57.  
  58.          The compiler will now recognise the procedure when it is
  59.          called.  }  
  60.  
  61.     Procedure Getnumber 
  62.         
  63.         (whichone: string; Var userchoice: real);forward;
  64.  
  65. {    ^
  66.     |
  67.     |__  This procedure accepts parameters.  These are outlined now,
  68.          and do not have to be defined again when the procedure is
  69.          given in full.  }
  70.  
  71.      Procedure Title; forward;
  72.  
  73.     { ---------------------------------------------------------- }
  74.  
  75.     Procedure Calculate;
  76.  
  77.     { Calls "Getnumber" procedure twice and multiplies the inputs }
  78.  
  79.     Var
  80.  
  81.     Rate,Hours: real;
  82.  
  83.     begin
  84.  
  85.     Getnumber('hours worked',Hours);
  86.  
  87. {    ^
  88.     |
  89.     |__  Now here's an interesting thing!  "Getnumber" hasn't been
  90.          defined yet!  It's OK however, because we have given the
  91.          compiler some indication that it exists by using the forward
  92.          declaration.
  93.  
  94.          The other weird thing here is that "Getnumber" is passed an
  95.          undefined string ('hours worked').  In other words, unless a
  96.          parameter is passed as a variable to be changed by a
  97.          procedure, any parameter will do as long as it is of the same
  98.          variable type.  In this case, "Getnumber" is expecting a
  99.          string.  Whether or not that string is passed directly (as is
  100.          the case here), or defined first as a variable and then
  101.          passed...
  102.  
  103.  
  104.         Var
  105.  
  106.         alternative: string[20];
  107.  
  108.         begin
  109.  
  110.         alternative := 'hours worked';
  111.  
  112.         Getnumber(alternative,hours);
  113.  
  114.         end
  115.  
  116.  
  117.          ...is unimportant!  This means that any parameter just passed
  118.          to a procedure, which will not be altered by that procedure, can
  119.          be sent without defining it first as a variable.  I think I'll
  120.          go and have a lie down.
  121.  
  122.          For example, let's say that a procedure takes two numbers...
  123.  
  124.  
  125.         Procedure elk (first: real; second: integer);
  126.  
  127.  
  128.          ...we can send it numbers like this...
  129.  
  130.  
  131.         number1 := 2.45;
  132.  
  133.         number2 := 8;        
  134.  
  135.         elk(number1,number2);
  136.  
  137.  
  138.          ...or like this...
  139.  
  140.  
  141.         elk(2.45,8);
  142.  
  143.  
  144.          ...which is so beautiful I think it calls for a celebratory
  145.          leg waxing.  }
  146.  
  147.     Getnumber('rate of pay',Rate);
  148.  
  149. {    ^
  150.     |
  151.     |__  Here we call "Getnumber" with a different string.  You'll see
  152.          why when you read the definition for the procedure below.  }
  153.  
  154.     writeln('     The amount due is $',Rate*Hours:0:2);
  155.     
  156.     writeln;
  157.     
  158.     end; { Calculate }
  159.  
  160.     { ---------------------------------------------------------- }
  161.  
  162.     Procedure Getnumber;
  163.  
  164. {    ^
  165.     |
  166.     |__  We've already outlined all the parameter passing information
  167.          in our "forward" declaration.  We could write it all out again
  168.          here, but what's the point?  (It'll teach you some humility
  169.          and be great for outdoor barbeques) }
  170.  
  171.     { Prompts user for a number }
  172.  
  173.     begin
  174.  
  175.     write('     Please enter the ',whichone,': ');
  176.  
  177. {                       ^
  178.                        |
  179.                        |__  Ah!  The point of sending a
  180.                         string becomes apparent.  Now
  181.                         "Getnumber" is tailored for
  182.                         whatever information you need! 
  183.                         "Whichone" defines the required
  184.                         data...  
  185.  
  186.     readln(userchoice);
  187.     
  188.     writeln;
  189.  
  190.     end; { Getnumber }
  191.     
  192.     { ---------------------------------------------------------- }
  193.  
  194.     Procedure Title;
  195.  
  196.     { Prints up a nice title on the screen }
  197.  
  198.     Const
  199.  
  200.     tab = '     ';
  201.  
  202.     begin
  203.  
  204.     writeln;
  205.  
  206.     writeln(tab,'PAY CALCULATOR');
  207.  
  208.     writeln(tab,'--------------');
  209.  
  210.     writeln;
  211.  
  212.     writeln(tab,'This program will calculate the pay due, given');
  213.  
  214.     writeln;
  215.  
  216.     writeln(tab,'the hours worked and rate of pay...');
  217.     
  218.     writeln;
  219.  
  220.     end; { Title }
  221.  
  222.     { ---------------------------------------------------------- }
  223.     { ---------------------------------------------------------- }
  224.  
  225.     begin { Main Program }
  226.  
  227.     Title;
  228.  
  229.     Calculate;
  230.  
  231.     end. { Main Program }
  232.  
  233.     { ---------------------------------------------------------- }
  234.  
  235. {  Exquisite!  I was never this forward before I began Pascal programming,
  236.    and I hope it works for you too!
  237.  
  238.    Notice that there are no global variables in this program.  Is that
  239.    lunar or what?
  240.  
  241.    The next series of tutorials will concentrate on wine making and
  242.    rocketry.  I look forward to boring you then... 
  243.  
  244.   
  245.                                            
  246.                                            
  247.                                            
  248.                                            
  249.                                            
  250.                                         
  251.                                           
  252.                                             
  253.                                               
  254.                                               
  255.                                             
  256.                                           
  257.                                          
  258.                                          
  259.                                             
  260.                                              
  261.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
  262.              «»                                                  «» 
  263.              «»            Propellus headus maximus              «» 
  264.              «»                                                  «» 
  265.              «»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«» 
  266.  
  267.  
  268.          43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43  !¡  43
  269.  
  270.  
  271.                                                                      }
  272.  
  273.  
  274.